package org.sbbs.security.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.sbbs.base.model.BaseTreeNode;

@Entity
@Table(name = "t_menu")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Menu extends BaseTreeNode<Menu, Long> {
	/**
	 *
	 */
	private static final long serialVersionUID = -779153086073086694L;
	// private Long id;
	private String title;
	private UrlResource urlResource;

	/*
	 * @Id
	 *
	 * @GeneratedValue(strategy = GenerationType.AUTO) public Long getId() {
	 * return id; } public void setId(Long id) { this.id = id; }
	 */
	@Column(nullable = false)
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@OneToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "urlResource_id", nullable = true)
	public UrlResource getUrlResource() {
		return urlResource;
	}

	public void setUrlResource(UrlResource urlResource) {
		this.urlResource = urlResource;
	}

	@Override
	public String toString() {
		return "Menu [title=" + title + ", urlResource=" + urlResource + "]";
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Menu other = (Menu) obj;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		if (urlResource == null) {
			if (other.urlResource != null)
				return false;
		} else if (!urlResource.equals(other.urlResource))
			return false;
		return true;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		result = prime * result
				+ ((urlResource == null) ? 0 : urlResource.hashCode());
		return result;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return this.getPK();
	}
}
